#!/bin/bash

. usage.sh

#USAGE : faustremote <servurl> ==> <targets>
#USAGE : faustremote [<servurl>] <platform> <arch> <srcfile> ==> <binary.zip>

#URL="https://faustservicecloud.grame.fr"
URL="https://faustservice.inria.fr"

function targetrequest {
    echo "Server URL:" $1
    curl  "$1/targets"
}

function compilation {
    curl -F"file=@\"$4\";filename=\"$4\""  "$1/compile/$2/$3/binary.zip" --output binary.zip
}

if [ "$1" == "-h" ]; then
    echo "Access the remote compilation service to compile DSP programs"
    echo "Usage: faustremote [<servurl>] [<platform> <arch> <srcfile>]"
    echo "faustremote <servurl> ==> <targets>"
    echo "faustremote <servurl> <platform> <arch> <srcfile> ==> <binary.zip>"
    echo "When no <servurl> is defined, the default GRAME Faust URL service is used"
elif [ "$#" == "0" ]; then
    targetrequest "$URL"
elif [ "$#" == "1" ]; then
    targetrequest "$1"
elif [ "$#" == "3" ]; then
    if [ -f "$3" ]; then
        rm -rf binary.zip
        compilation $URL $1 $2 $3
    else 
        echo "ERROR: file $3 doesn't exist"
        exit 1
    fi
elif [ "$#" == "4" ]; then
    if [ -f "$4" ]; then
        rm -rf binary.zip
        compilation $1 $2 $3 $4
    else 
        echo "ERROR: file $4 doesn't exist"
        exit 1
    fi
fi 

